<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            left: 100px;
            top: 100px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
</body>
<script>
    // 鼠标跟随:  盒子box跟随鼠标在文档中移动
    // 1. 鼠标的位置
    // 2. 盒子跟随鼠标移动 => 跳转盒子的位置

    var box = document.getElementsByClassName("box")[0];

    document.onmousemove = function (e) {
        var e = e || window.event;
        var x = e.pageX - box.clientWidth / 2;
        var y = e.pageY - box.clientHeight / 2;

        // 取值时记得去单位   赋值时记得加单位
        box.style.left = x + "px";
        box.style.top = y + "px";

    }


</script>

</html>